package cn.lianglw.liangapiadmin.manager;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;

/**
 * Cos 对象存储操作
 *
 */
@Component
@Slf4j
public class OssManager {

    @Value("${aliyun.oss.urlPrefix}")
    private String urlPrefix;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    @Value("${aliyun.oss.endpoint}")
    private String endPoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    


    /**
     * 上传到OSS服务器 如果同名文件会覆盖服务器上的
     *
     * @param bytes 文件流
     * @param fileName 文件名称 包括后缀名
     * @return 出错返回"" ,唯一MD5数字签名
     */
    public String uploadFile2OSS(byte[] bytes, String fileName) {

        OSS oss = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);

        try {
            // 创建PutObject请求。
            oss.putObject(bucketName, fileName, new ByteArrayInputStream(bytes));
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (oss != null) {
                oss.shutdown();
            }
        }


        log.info("文件上传到:{}", urlPrefix + fileName);

        return urlPrefix + fileName;
    }

    /**
     * Description: 判断OSS服务文件上传时文件的contentType
     *
     * @param filenameExtension 文件后缀
     * @return String
     */
//    public static String getContentType(String filenameExtension) {
//        if (filenameExtension.equalsIgnoreCase("bmp")) {
//            return "image/bmp";
//        }
//        if (filenameExtension.equalsIgnoreCase("gif")) {
//            return "image/gif";
//        }
//        if (filenameExtension.equalsIgnoreCase("jpeg") || filenameExtension.equalsIgnoreCase("jpg")
//                || filenameExtension.equalsIgnoreCase("png")) {
//            return "image/jpeg";
//        }
//        if (filenameExtension.equalsIgnoreCase("html")) {
//            return "text/html";
//        }
//        if (filenameExtension.equalsIgnoreCase("txt")) {
//            return "text/plain";
//        }
//        if (filenameExtension.equalsIgnoreCase("vsd")) {
//            return "application/vnd.visio";
//        }
//        if (filenameExtension.equalsIgnoreCase("pptx") || filenameExtension.equalsIgnoreCase("ppt")) {
//            return "application/vnd.ms-powerpoint";
//        }
//        if (filenameExtension.equalsIgnoreCase("docx") || filenameExtension.equalsIgnoreCase("doc")) {
//            return "application/msword";
//        }
//        if (filenameExtension.equalsIgnoreCase("xml")) {
//            return "text/xml";
//        }
//        return "application/octet-stream";

}